using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using ShoppingCenter.Backend.Api.Model;
using ShoppingCenter.Backend.Api.Params;
using ShoppingCenter.Backend.Api.Repository;
using ShoppingCenter.Backend.Api.Utils;

namespace ShoppingCenter.Backend.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]

    //购物车控制器
    public class ShoppingCartsController : ControllerBase
    {
        private IConfiguration _configuration;

        private IRepository<Shopping_Cart> _shoppingCartRepository;

        // 用户
        private IRepository<User> _usersRepository;

        //商品
        private IRepository<Merchandise> _merchandiseRepository;
        // 商品详情
        private IRepository<Details> _detailsRepository;

        // 商家
        private IRepository<Merchant> _merchantRepository;


        public ShoppingCartsController(IConfiguration configuration, IRepository<Details> detailsRepository, IRepository<Merchant> merchantRepository, IRepository<Merchandise> merchandiseRepository, IRepository<User> usersRepository, IRepository<Shopping_Cart> shoppingCartRepository)
        {
            _configuration = configuration;
            _shoppingCartRepository = shoppingCartRepository;
            _usersRepository = usersRepository;
            _merchandiseRepository = merchandiseRepository;
            _merchantRepository = merchantRepository;
            _detailsRepository = detailsRepository;
        }


        //获取所有购物车
        public dynamic Get()
        {
            var currentPage = int.Parse(Request.Query["currentPage"][0]);
            var pageSize = int.Parse(Request.Query["pageSize"][0]);

            var shoppingCarts = _shoppingCartRepository.Table.ToList();
            // 分页
            var shoppingCart = shoppingCarts.ToList().Skip((currentPage - 1) * pageSize).Take(pageSize); ;

            return new
            {
                Code = 1000,
                Data = new
                {
                    Data = shoppingCart,
                    Pager = new { currentPage, pageSize, totalSize = shoppingCarts.Count() }
                },
                Msg = "获取所有用户的购物车成功"
            };
        }


        //根据用户id获取购物车
        [HttpGet("{userId}")]

        public dynamic Get(int userId)
        {

            var currentPage = int.Parse(Request.Query["currentPage"][0]);
            var pageSize = int.Parse(Request.Query["pageSize"][0]);

            var currentUserShoppingCar = _shoppingCartRepository.Table.Where(x => x.UserId == userId).ToList();

            var merchandise = _merchandiseRepository.Table.ToList();
            var details = _detailsRepository.Table.ToList();
            var merchant = _merchantRepository.Table.ToList();

            // 分页
            var shoppingCart = currentUserShoppingCar.ToList().Skip((currentPage - 1) * pageSize).Take(pageSize); ;

            return new
            {
                Code = 200,
                Data = new
                {
                    Data = shoppingCart,
                    Pager = new { currentPage, pageSize, totalSize = currentUserShoppingCar.Count() }
                },
                Msg = "获取指定用户的购物车成功"
            };

        }

        //添加购物车
        [HttpPost]
        public dynamic Post(CUR_ShoppingCart newShoppingCart)
        {
            // 当前用户id、当前商品id
            var user = _usersRepository.GetById(newShoppingCart.UserId);
            var merchandise = _merchandiseRepository.GetById(newShoppingCart.MerchandiseId);

            if (user == null || merchandise == null)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "当前用户或商品不存在"
                };
            }
            //购买数量
            var buyNum = newShoppingCart.BuyNum == 0 ? 1 : newShoppingCart.BuyNum;

            var ShoppingCart = new Shopping_Cart
            {
                UserId = user.Id,
                MerchandiseId = merchandise.Id,
                BuyNum = buyNum
            };

            _shoppingCartRepository.Insert(ShoppingCart);


            return new
            {
                Code = 1000,
                Data = ShoppingCart,
                Msg = "加入购物车成功"
            };
        }




    }
}